package com.jiazhong.basic.shuxin.design.代理模式.动态代理模式;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CGLibProxyHandler implements MethodInterceptor {

    private Object target;

    public Object setTarget(Object target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("开始了动态的CGLibProxyHandler");
        System.out.println(method.getName());
        System.out.println("结束了动态的CGLibProxyHandler");
        return method.invoke(target, objects);
    }
}
